<?php
/**
 * 文章类
 * User: Administrator
 * Date: 14-5-6
 * Time: 下午4:36
 */
class Article extends CI_Controller {
    function __construct() {
        parent::__construct ();
        $this->load->database ();
    }


    /**
     * 文章管理
     */
    function article_list()
    {
        $numPerPage = $this->input->post('numPerPage') >= 1 ? $this->input->post('numPerPage') : 20;
        $query = array();
        $query['query'] = $this->Marticle->getArticleAll('','',$numPerPage);
        $query['numPerPage'] = $numPerPage;

        $columnObj = $this->Mcolumn->loadColumn();
        foreach($columnObj as $key=>$value){
            $query['column'][$key]['id'] = $value->id;
            $query['column'][$key]['name'] = $value->name;
        }

        $this->load->view('article',$query);
    }

    function add_article()
    {


        $columnObj = $this->Mcolumn->loadColumn();
        $column = array();
        foreach($columnObj as $key=>$value){
            $column['column'][$key]['id'] = $value->id;
            $column['column'][$key]['name'] = $value->name;
        }



        $this->load->view('add_article',$column);
    }

    function loadArticle()
    {
        $title = $this->input->post('title');
        $column_id = $this->input->post('column_id');

        $query = $this->Marticle->getArticleAll($title,$column_id,20);
        //print_r($query['data']);
        $result = "";
        foreach ( $query['data'] as $row ) {
            $result .= "<tr>
					<td><input type='checkbox' value='" . $row['id'] . "' /></td>
							<td>".$row['id']."</td>
							<td>".$row['title']."</td>
							<td>".$row['columnName']."</td>
							<td>".$row['uid']."</td>
							<td>".$row['author']."</td>
							<td>".$row['dateTime']."</td>
							<td>".$row['sort']."</td>
							<td style='color:red;'>".($row['is_show'] == 1 ?'显示' : '隐藏')."</td>
							<td>
			<a href='javascript:void(0)' style='color:blue' onclick='modifyArticle(" . $row['id'] . ")'>编辑</a>
			&nbsp;&nbsp;&nbsp;&nbsp;
					<a href='javascript:void(0)' style='color:blue' onclick='delSelectMultiArticle(" . $row['id'] . ")'>删除</a></td>
							";
            $result .= "</tr>";
        }


        echo $result;

    }

    function addArticle()
    {
        $title      = $this->input->post('title');
        $content    = $this->input->post('content');
        $column_id  = (int)$this->input->post('column_id');
        $author     = $this->input->post('author');
        $keyword    = $this->input->post('keyword');
        $id         = (int)$this->input->post('id');
        $uid        = $this->input->post('uid');
        $url        = $this->input->post('url');
        $isShow     = (int)$this->input->post('show');
        $sort       = (int)$this->input->post('sort');

        if(empty($id)){

            $this->Marticle->addArticle($title,$content,$column_id,$author,$keyword,$uid,$url,$isShow,$sort);
        }else{
            $this->Marticle->editArticle($id,$title,$content,$column_id,$author,$keyword,$uid,$url,$isShow,$sort);
        }
        $data = array();
        $data['ReturnCode'] = 1;
        echo json_encode($data);
    }

    function loadNewArticle()
    {
        $id = $this->input->post('id');
        if(!empty($id)){
            $data = $this->Marticle->getArticleById($id);
        }

        echo json_encode($data);

    }

    function delArticle()
    {
        $ids = trim($this->input->post('ids'),',');

        $this->Marticle->deleteArticle($ids);
        $arr ["ReturnCode"] = 1;
        echo json_encode ( $arr );
    }

    function column()
    {
        $this->load->view('column');
    }

    function loadColumn() {
        $query = $this->Mcolumn->loadColumn ();
        $result = "";
        foreach ( $query as $row ) {
            $result .= "<tr>
					<td><input type='checkbox' value='" . $row->id . "' /></td>
							<td>$row->id</td>
							<td>$row->name</td>
							<td>
			<a href='javascript:void(0)' style='color:blue' onclick='modifyColumn(" . $row->id . ")'>编辑</a>
			&nbsp;&nbsp;&nbsp;&nbsp;
					<a href='javascript:void(0)' style='color:blue' onclick='delColumn(" . $row->id . ")'>删除</a></td>
							";
            $result .= "</tr>";
        }
        echo $result;
    }

    function add_column()
    {
        $this->load->view('add_column');
    }


    function checkColumn()
    {
        $id = $this->input->post('id');
        $columnName = $this->input->post('columnName');

        $return = $this->Mcolumn->checkColumnByName($columnName);
        $data = array();
        if(!empty($return['name']) && $return['id'] != $id){
            $data['ReturnCode'] = 0;
        }else{
            $data['ReturnCode'] = 1;
        }
        echo json_encode($data);
    }

    function addColumn()
    {
        $id = $this->input->post('id');
        $columnName = $this->input->post('columnName');

        if(empty($id)){
           $ReturnCode = $this->Mcolumn->addColumn($columnName);
        }else{
            $ReturnCode = $this->Mcolumn->editColumn($id,$columnName);
        }

        $data = array();
        if($ReturnCode){
            $data['ReturnCode'] = 1;
        }else{
            $data['ReturnCode'] = 0;
        }

        echo json_encode($data);
    }


    function getColumnById()
    {
        $id = $this->input->post('id');

        $return = $this->Mcolumn->getColumnById($id);

        echo json_encode($return);
    }

    function deleteColumn()
    {
        $ids = trim($this->input->post('ids'),',');

        $this->Mcolumn->deleteColumn($ids);
        $arr ["ReturnCode"] = 1;
        echo json_encode ( $arr );
    }

}